//
//  HRMingJiaViewController.m
//  Economical report
//
//  Created by wyzc on 16/6/27.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CELLID @"CELL"
#import "HRMingJiaViewController.h"
#import "AFNetworking.h"
#import "HRMingJiaModel.h"
#import "HRMingJiaCell.h"
#import "HRAllMingJiaViewController.h"
@interface HRMingJiaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSourceArr;
@end

@implementation HRMingJiaViewController
-(NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr==nil) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self sendRequestData];
    self.navigationController.navigationBar.barTintColor=HRCOLOR(0, 200, 130);
    //self.navigationItem.title=@"21世纪经济报道";
    self.tableView.rowHeight=88;
    [self createHeadView];
        //注册xib
    UINib *nib=[UINib nibWithNibName:@"HRMingJiaCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CELLID];
}
//添加tableviewHeadview
-(void)createHeadView
{
    UIView *headView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, UPSreenW, 88)];
    self.tableView.tableHeaderView=headView;
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
    imgView.image=[UIImage imageNamed:@"nav_user"];
    [headView addSubview:imgView];
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(80, 10, 120, 60)];
    titleLabel.text=@"所有名家";
    [headView addSubview:titleLabel];
     imgView=[[UIImageView alloc]initWithFrame:CGRectMake(UPSreenW-70, 10, 60, 60)];
    imgView.image=[UIImage imageNamed:@"list_arrows2_iPhone"];
    [headView addSubview:imgView];
    //添加点击手势
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTapsRequired=1;//点击次数
    tap.numberOfTouchesRequired=1;//点击手指数
    [headView addGestureRecognizer:tap];
    
    

}
//点击实现事件
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
    HRAllMingJiaViewController *allMingJia=[[HRAllMingJiaViewController alloc]init];
    [self.navigationController pushViewController:allMingJia animated:YES];
    
}
-(void)sendRequestData
{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:@"http://api.21jingji.com/author/articleList?page=1" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseDict) {
        NSArray *arrList=responseDict[@"list"];
        for (NSDictionary *dict in arrList) {
            HRMingJiaModel *model=[HRMingJiaModel modelWithDict:dict];
            [self.dataSourceArr addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
#pragma mark--tableView的数据源代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRMingJiaModel *model=self.dataSourceArr[indexPath.row];
    HRMingJiaCell *cell=[tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.model=model;
    return cell;
}
@end
